/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Dec 16, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.tool;

import edu.psu.geovista.app.gmatrix.JDfMatrix;
import edu.psu.geovista.app.gmatrix.app.SimpleSortableMatrix;
import edu.psu.geovista.app.gmatrix.data.DfMatrixModel;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;



public class DfMatrixRowExportor {

        protected JDfMatrix destMatrix;  //SimpleSortableMatrix
        protected JFrame frm;
        boolean setcolname=true;

        public DfMatrixRowExportor() {
            frm=createFrame("Similar Observations");
            destMatrix =this.createMatrix();
        }



        /**
         *
         * @param srcMatrix      export rows from this matrix
         * @param selectRows   index of matrix rows being exported
         * @param newMatrix      put the result in a new matrix
         * @param clear       true, clear previous selection; false, keep previous selection
         */
        public  JDfMatrix export(JDfMatrix srcMatrix, int[] selectRows, boolean newMatrix, boolean clear,String frmtitle) {
           JDfMatrix destMatrix =this.getDestinationMatrix(newMatrix); //export rows to this matrix
           JFrame frm=this.getFrame(newMatrix,frmtitle);
            frm.getContentPane().add(destMatrix);

            if(clear){//remove previous selection
                DfMatrixModel dm = (DfMatrixModel) destMatrix.getDatamodel();
                dm.clear();
            }

            DfMatrixModel datamodel = (DfMatrixModel) srcMatrix.getDatamodel();
            String[] colnames = datamodel.getColumnNames();


            destMatrix.setColnames(colnames);
            /*if(setcolname) {
                setcolname=false;
            }*/

            Vector dataVector = datamodel.getDataVector();




            for (int i=0;i<selectRows.length ;i++){
                String rowName = datamodel.getRowName(selectRows[i]);
                Vector arow = (Vector) dataVector.get(selectRows[i]);
                destMatrix.addRow(rowName,arow);
            }

            show(frm);
            return destMatrix;
        }

        protected  JFrame getFrame(boolean newfrm,String title){
            if(newfrm){
                return createFrame(title);
            }
            else{
               if(title!=null&&title.length() >0){
                   frm.setTitle(title);
               }
               return frm;
            }
        }

    public JDfMatrix getDestMatrix() {
        return destMatrix;
    }

    protected  JDfMatrix getDestinationMatrix(boolean newmatrix){
            if(newmatrix){
                return createMatrix();
            }
            else{
                return this.destMatrix ;
            }
        }

        protected  JFrame createFrame(String  title) {
            //SimpleSortableMatrix mymatrix = createMatrix();

            JFrame frm=new JFrame(title);
            Container container = frm.getRootPane().getContentPane();
            //container.add(mymatrix);
            frm.setSize(new Dimension(800,600));
            return frm;
        }

        protected  JDfMatrix createMatrix() {
            int columnHeaderHight = 30;
            int rowHeaderWidth = 40;
            SimpleSortableMatrix mymatrix = new SimpleSortableMatrix(columnHeaderHight, rowHeaderWidth) ;
            mymatrix.setRowColumn(0,12);//12: 12 years
            return mymatrix;
        }

        protected  void show(JFrame frm){
            frm.setVisible(true);
        }

    /**
     * when rows export from src to dest, the row indexs of the two matrix do not match anymore. The rows btw the 2 matrix
     * can only be mapped via rowname. This method is to convert rowindex btw the matrix.
     *  
     * @param src
     * @param dest
     * @param srcCells
     * @return  cell index of cell in dest matrix.
     */
        public static Set <Point> convertCellrowindexs(JDfMatrix src,JDfMatrix dest, Set <Point> srcCells){
            Set destcells=new HashSet(srcCells.size() );
        for (Iterator iterator = srcCells.iterator(); iterator.hasNext();) {
            Point srcIndex = (Point) iterator.next();
            Point destIndex = convertCellrowindex(src,  dest,srcIndex);
            destcells.add(destIndex);
        }
        return destcells;
        }

    public static Point convertCellrowindex(JDfMatrix src, JDfMatrix dest,Point srcIndex ) {
        String rowName = src.getRowName(srcIndex.x);
        int destRowindex = dest.getRowIndexByName(rowName);
        Point destIndex=new Point(destRowindex,srcIndex.y);
        return destIndex;
    }

}

